package com.hanthink.system.service.impl.manhour;

import java.util.List;

import com.hanthink.common.core.domain.entity.SysDept;
import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.DateUtils;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.manhour.ImportEntity.PLineMachineStructureImportEntity;
import com.hanthink.system.domain.manhour.PLineEquipment;
import com.hanthink.system.domain.pc.EmEquip;
import com.hanthink.system.mapper.EmEquipMapper;
import com.hanthink.system.mapper.manhour.PLineEquipmentMapper;
import com.hanthink.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hanthink.system.mapper.manhour.PLineMachineStructureMapper;
import com.hanthink.system.domain.manhour.PLineMachineStructure;
import com.hanthink.system.service.manhour.IPLineMachineStructureService;

/**
 * 产线的设备维护Service业务层处理
 *
 * @author hanthink
 * @date 2022-05-05
 */
@Service
public class PLineMachineStructureServiceImpl implements IPLineMachineStructureService {



    private static final Logger log = LoggerFactory.getLogger(PLineMachineStructureServiceImpl.class);
    @Autowired
    private PLineMachineStructureMapper pLineMachineStructureMapper;


    @Autowired
    private EmEquipMapper emEquipMapper;


    @Autowired
    private PLineEquipmentMapper pLineEquipmentMapper;


    /**
     * 查询产线的设备维护
     *
     * @param id 产线的设备维护ID
     * @return 产线的设备维护
     */
    @Override
    public PLineMachineStructure selectPLineMachineStructureById(Long id) {
        return pLineMachineStructureMapper.selectPLineMachineStructureById(id);
    }

    /**
     * 查询产线的设备维护列表
     *
     * @param pLineMachineStructure 产线的设备维护
     * @return 产线的设备维护
     */
    @Override
    public List<PLineMachineStructure> selectPLineMachineStructureList(PLineMachineStructure pLineMachineStructure) {
        return pLineMachineStructureMapper.selectPLineMachineStructureList(pLineMachineStructure);
    }

    /**
     * 根据部门id和产线id查询设备列表
     *
     * @param deptid  部门id
     * @param plineid 产线id
     * @return 设备列表清单
     */
    @Override
    public List<PLineMachineStructure> selectPLineMachineStructureListbyfactoryid(String deptid, String plineid) {
        return pLineMachineStructureMapper.selectPLineMachineStructureListbyfactoryid(deptid, plineid);
    }

    /**
     * 根据部门id和产线名称查询设备列表
     *
     * @param deptid    部门id
     * @param plinename 产线名称
     * @return 设备列表清单
     */
    @Override
    public List<PLineMachineStructure> selectPLineMachineStructureListbyfactoryname(String deptid, String plinename) {
        return pLineMachineStructureMapper.selectPLineMachineStructureListbyfactoryname(deptid, plinename);
    }


    /**
     * 新增产线的设备维护
     *
     * @param pLineMachineStructure 产线的设备维护
     * @return 结果
     */
    @Override
    public int insertPLineMachineStructure(PLineMachineStructure pLineMachineStructure) {
        return pLineMachineStructureMapper.insertPLineMachineStructure(pLineMachineStructure);
    }

    /**
     * 修改产线的设备维护
     *
     * @param pLineMachineStructure 产线的设备维护
     * @return 结果
     */
    @Override
    public int updatePLineMachineStructure(PLineMachineStructure pLineMachineStructure) {
        return pLineMachineStructureMapper.updatePLineMachineStructure(pLineMachineStructure);
    }

    /**
     * 批量删除产线的设备维护
     *
     * @param list 需要删除的产线的设备维护ID
     * @return 结果
     */
    @Override
    public int deletePLineMachineStructureByIds(List<PLineMachineStructure> list) {
        return pLineMachineStructureMapper.deletePLineMachineStructureByIds(list);
    }

    /**
     * 删除产线的设备维护信息
     *
     * @param id 产线的设备维护ID
     * @return 结果
     */
    @Override
    public int deletePLineMachineStructureById(Long id) {
        return pLineMachineStructureMapper.deletePLineMachineStructureById(id);
    }


    /**
     * @param planList
     * @param operName
     * @Description: 产线的设备维护信息导入
     * @return: java.lang.String
     * @Author: 唐子越
     * @Date: 2023/7/6 11:30
     */
    @Override
    public String importPlan(List<PLineMachineStructureImportEntity> planList, String operName) {


        if (StringUtils.isNull(planList) || planList.size() == 0) {
            throw new CustomException("导入计划数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int currentRowNum = 0;

        List<SysDept> allFactory = pLineMachineStructureMapper.getAllFactory();

        for (PLineMachineStructureImportEntity importEntity : planList) {
            currentRowNum++;

            //检查 导入数据的信息
            try {
                String factoryName = importEntity.getFactoryName();
                SysDept sysDept = checkFactory(allFactory, factoryName);
                if (sysDept == null) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据 工厂名称不正确");
                    continue;
                }

                String machineid = importEntity.getMachineid();
                if (StringUtils.isEmpty(machineid)) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据 设备代码不能为空");
                    continue;
                }

                EmEquip emEquip = emEquipMapper.selectEmEquipByCode(machineid);
                if (emEquip == null) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据 设备代码不存在");
                    continue;
                }


                String plineid = importEntity.getPlineid();
                if (StringUtils.isEmpty(machineid)) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据 生产线代码不能为空");
                    continue;
                }

                PLineEquipment pLineEquipment = pLineEquipmentMapper.selectPLineEquipmentById(plineid);
                if (pLineEquipment == null) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第" + currentRowNum + "行数据 生产线代码不存在");
                    continue;
                }

                //获取主键对应的数据
                PLineMachineStructure oldEntity = pLineMachineStructureMapper
                        .selectPlineMachineStructureByMachineIdAndPlineId(machineid, plineid);

                if (oldEntity == null) {
                    //新增
                    PLineMachineStructure pLineMachineStructure = new PLineMachineStructure();
                    pLineMachineStructure.setFactoryid(sysDept.getDeptId().toString());
                    pLineMachineStructure.setPlineid(plineid);
                    pLineMachineStructure.setPlinename(pLineEquipment.getPlinename());
                    pLineMachineStructure.setMachineid(machineid);
                    pLineMachineStructure.setMachinename(emEquip.getDesc());
                    pLineMachineStructure.setCreateBy(operName);
                    pLineMachineStructure.setCreateTime(DateUtils.getNowDate());
                    pLineMachineStructureMapper.insertPLineMachineStructure(pLineMachineStructure);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、产线的设备维护信息  设备代码:" + machineid + "，生产线代码:" + plineid + " 导入成功");
                } else {
                    //更新
                    oldEntity.setFactoryid(sysDept.getDeptId().toString());
                    oldEntity.setPlineid(plineid);
                    oldEntity.setPlinename(pLineEquipment.getPlinename());
                    oldEntity.setMachineid(machineid);
                    oldEntity.setMachinename(emEquip.getDesc());
                    oldEntity.setUpdateBy(operName);
                    oldEntity.setUpdateTime(DateUtils.getNowDate());
                    pLineMachineStructureMapper.updateImportData(oldEntity);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、产线的设备维护信息  设备代码:" + machineid + "，生产线代码:" + plineid + " 更新成功");

                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、产线的设备维护信息  设备代码:" + importEntity.getMachineid() + "，生产线代码:" + importEntity.getPlineid() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }


        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }


    private SysDept checkFactory(List<SysDept> allFactory, String factoryName) {

        if (StringUtils.isEmpty(factoryName)) {
            return null;
        }

        for (SysDept dept : allFactory) {
            String deptName = dept.getDeptName();
            if (deptName.contains(factoryName.trim())) {
                return dept;
            }
        }
        return null;
    }
}
